/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author triki
 */
public class Servidor {

    private ServerSocket mainSocket;
    private int maxClients;
    private int act_clients;
    private int port;
    private ConcurrentHashMap userLookUp; //guardamos la IP y puerto
    private ConcurrentHashMap userRegistry; // guardamos las canciones de cada usuario.

    private String path_base;

    public Servidor(int max,int port,String path_base)
    {
        maxClients = max;
        act_clients = 0;
        userLookUp = new ConcurrentHashMap();
        userRegistry = new ConcurrentHashMap();
        this.port = port;
        this.path_base = new String(path_base);
        
        File f = new File(this.path_base);
        //Si no existe la carpeta, la crea.
        if(!f.exists())
            f.mkdir();
    }

    public void startServer()
    {
        try
        {
            mainSocket = new ServerSocket(port);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
            System.exit(-1);
        }
    }

    public Socket acceptClient()
    {
        try
        {
            Socket sock = mainSocket.accept();
            return sock;
        }
        catch (IOException ex)
        {
            return null;
        }
    }

    public int getMaxUsersAccepted()
    {
        return maxClients;
    }

    public ConcurrentHashMap getUserLookUp()
    {
        return userLookUp;
    }

    public ConcurrentHashMap getUserRegistry()
    {
        return userRegistry;
    }

    public String getPathBase()
    {
        return this.path_base;
    }
}
